package com.wzx.ptplatform.controller.admin.company;

import com.wzx.ptplatform.domain.WorkDetail;
import com.wzx.ptplatform.service.WorkDetailService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员企业模块——兼职信息工作详情管理")
@RestController
public class AdminCompanyWorkDetailController {

    final WorkDetailService workDetailService;

    public AdminCompanyWorkDetailController(WorkDetailService workDetailService) {
        this.workDetailService = workDetailService;
    }

    @RequestMapping("admin_company_work_detail_message_list")
    public LayuiPager<WorkDetail> adminCompanyWorkDetailMessageList(
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        WorkDetail workDetail = new WorkDetail();
        Page<WorkDetail> pager = workDetailService.findList(workDetail, page - 1, limit);
        LayuiPager<WorkDetail> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_company_work_detail_message_editView")
    public ModelAndView adminCompanyWorkDetailMessageEditView(String id, Model model) {
        model.addAttribute("workDetail", workDetailService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/company/pt_work_detail_message_edit");
    }

    @PostMapping("admin_company_work_detail_message_save_or_edit")
    public Map<String, Object> adminCompanyWorkDetailMessageSaveOrEdit(WorkDetail workDetail) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (workDetailService.saveOrEdit(workDetail)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_company_work_detail_message_delete")
    public Map<String, Object> adminCompanyWorkDetailMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (workDetailService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

}
